DM 'CLEAR LOG; CLEAR OUTPUT;';
title '      ';
libname ecrit '..';

options sortsize=max ubufsize=8M nothreads;

data test;
set ecrit.base_94_14_hors_euroTVA_chg_ns;
idpostep2=0;
idpostep2=idposte_p;
run;
proc freq data=test;
tables annee;
run;

options nonotes nomprint; 

%let nboot=100; 
%macro tirage_boot_codeserie(nbsim, data); 
%do nsim=1 %to &nbsim; 
  data tab_boot;
    retain ii 0;
	num_obs=int(ranuni(1510+&nsim)*tot_obs)+1;
  	set &data(keep=id) point=num_obs nobs=tot_obs;
	Const=1;
  	ii+1; if ii>tot_obs then stop;
  run;
  
  proc summary data=tab_boot nway; 
  class id; 
  var Const; 
  output out=/*boot.*/struct_idposte&nsim(keep=id nid) n=nid;
  run; 
%end; 
%mend; 

%macro boot_kurto(data,nbsim, idposte, outlier_min, outlier_max);

proc sort data=&data;
by id; 
run; 

dm output "clear";
%put(&idposte); 

%do nsim=1 %to &nbsim; 


/*dm log "clear";*/

data &data.b(drop=id rename=(idn=id)); 
merge &data(in=a) struct_idposte&nsim(in=b keep=id nid); 
by id; 
if a & b then do; 
  do k=1 to nid; 
    idn=id||k; 
	output;
  end; 
end; 

proc univariate data=&data.b noprint;
by idpostep2;
var dln_prix_ns;
output out=stat_o pctlpts=&outlier_min &outlier_max pctlpre=P;
run;


data &data.b;
set  stat_o &data.b;
by idpostep2;
/*if a;*/
run;

data &data.b; 
set &data.b;
by idpostep2;
retain p1b p99b;
if first.idpostep2 then do; 
  p1b=p1;
p99b=p99;
end; 
output;
run;


proc means data=&data.b (where=(dln_prix_ns>p1b & dln_prix_ns<p99b)) noprint;
var dln_prix_ns a_dprix_ns;
output out=stat1 median=q2 q2b q1=q1 q1b q3=q3 q3b kurtosis=kurt kurtb;
run;

proc univariate data=&data.b (where=(dln_prix_ns>p1b & dln_prix_ns<p99b)) noprint;
var dln_prix_ns ;
output out=stat2 pctlpre=dlnp_ns_ pctlpts=10,25,50,75,90; 
run; 


data stat2;
set stat2;
rename dlnp_ns_10=p10;
rename dlnp_ns_90=p90;
run;

data stat;
merge stat1 stat2;
run;

data stat;
set stat;
interq=q3-q1;
keep kurt q1 q3 q2 interq p10 p90;
run;

data ecrit.kurt;
set ecrit.kurt stat(in=b);
if b then do;
nsim=&nsim;
idposte1=&idposte; 
end;run; 

%end; 
%mend; 


%macro boucle_idposte(liste); 
%let l=1; %let idposte=%scan(&liste,&l); 
%do %while(%length(&idposte)>0); 

data tab_obs;
set test;
id=_N_;
if idpostep2 ne &idposte then delete;
run;

data tab_obs1;
set tab_obs;
keep id ;
run;

%tirage_boot_codeserie(&nboot, tab_obs1);
%boot_kurto(tab_obs, &nboot, &idposte, 1, 99);

  %let l=%eval(&l+1); 
  %let idposte=%scan(&liste,&l); 
%end; 
%mend; 
%let liste_idposte= 11111	11121	11122	11131	11132	11133	11141	11142	11143	11144	11211	11212	11213	11221	11222	11231	11232
11241	11242	11243	11244	11245	11246	11251	11252	11253	11261	11262	11263	11321	11322	11411	11412	11421	11422	11431	11432
11433	11434	11435	11441	11511	11521	11522	11621	11721	11722	11723	11811	11812	11813	11821	11822	11831	11911	11921	11922
11931	11932	12111	12121	12122	12131	12211	12221	12222	12223	21111	21112	21121	21122	21211	21212	21213	21221	21222	21311
31111	31211	31212	31213	31214	31215	31221	31222	31223	31224	31225	31226	31231	31232	31233	31241	31242	31251	31252	31253
31254	31261	31262	31263	31264	31271	31272	31273	31274	31311	31312	31411	31412	32111	32112	32113	32121	32122	32123	43111
43112	43113	43211	43221	44111	44131	44141	45221	45311	45411	51111	51112	51113	51121	51122	51123	51131	51141	51151	51152
51153	51154	51211	52111	52112	52113	52121	52122	53111	53112	53121	53131	53141	53142	53211	53311	54111	54112	54121	54122
54131	55111	55112	55113	55114	55115	56111	56112	56121	56122	56221	56222	61121	61131	61132	62313	62314	71211	71212	72111
72121	72122	72211	72212	72221	72311	72321	72322	72412	72421	72422	73211	73611	73612	81211	91111	91112	91113	91211	91212
91311	91411	91412	91413	91511	92111	92112	92113	93111	93211	93212	93321	93322	93411	93412	93413	94111	94112	94113	94211
94221	94222	94241	94242	95111	95112	95113	95211	95311	95312	95321	95322	101111	101112	101113	111111	111112	111121	111122	111123
111124	111125	111211	111221	112111	112112	112121	112132	121111	121112	121121	121311	121312	121321	121322	121331	121332	121333	123111	123112
123211	123221	123222	123223	124121	127111	127123	999999;

%boucle_idposte(&liste_idposte);


